//
//  TVCommodityServiceCell.swift
//  iPhone
//
//  Created by Toj on 8/10/22.
//

import UIKit

private let buyButtonHeight: CGFloat = 32

// MARK: - 懒加载(属性), 变量, 初始化init
class TVServiceCell: UICollectionViewCell {
    
    var model: TVServiceCellModel? { didSet {
        guard let service = model else { return }
        iconImageView.image = service.iconName.to_image
        nameLabel.text = service.name
    } }
    
    @objc private func didTapCell() {
        STUserModel.shared.loginTo(targetUrl:model?.targetUrl ?? "")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapCell))
        contentView.addGestureRecognizer(tapGesture)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()

        setupMakeLayoutSubviews()
    }
    
    private lazy var iconImageView: UIImageView = UIImageView()
    
    private lazy var arrowsImageView: UIImageView = {
       let view = UIImageView()
        view.image = "commodity_arrows".to_image
        return view
    }()
    
    private lazy var nameLabel: UILabel = {
        let view = UILabel()
        view.font = UIFont.systemFont(ofSize: 16)
        view.textColor = "#333333".to_color
        return view
    }()
    
    // 横线
    private lazy var lineView: UIView = {
       let view = UIView()
        view.backgroundColor = "#EEEEEE".to_color
        return view
    }()
}

// MARK: - 初始化
extension TVServiceCell {
    
    private func setupMakeAddSubviews() {
        
        contentView.addSubview(iconImageView)
        contentView.addSubview(arrowsImageView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(lineView)
    }
    
    private func setupMakeLayoutSubviews() {
        
        iconImageView.snp.makeConstraints { make in
            make.left.centerY.equalTo(contentView)
            make.height.equalTo(32)
            make.width.equalTo(32)
        }
        
        arrowsImageView.snp.makeConstraints { make in
            make.right.centerY.equalTo(contentView)
            make.height.equalTo(24)
            make.width.equalTo(24)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.centerY.equalTo(contentView)
            make.left.equalTo(iconImageView.snp.right).offset(8)
            make.right.equalTo(arrowsImageView.snp.left).offset(-8)
        }
        
        lineView.snp.makeConstraints { make in
            make.height.equalTo(1)
            make.left.equalTo(iconImageView)
            make.right.equalTo(arrowsImageView)
            make.bottom.equalTo(contentView)
        }
    }
}
